﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;


public partial class sysadmin_QuestionManager_CelueSelectQuestion : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerCourseInfo objManagerCourseInfo = new ManagerCourseInfo();
    //ManagerPloyOptionInfo objManagerPloyOptionInfo = new ManagerPloyOptionInfo();
    //ManagerPaperInfo objManagerPaperInfo = new ManagerPaperInfo();

    ManagerPaper objManagerPaper = new ManagerPaper();
    Course objCourse = new Course();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["PaperID"] = Request.QueryString["p"].ToString();
            ViewState["UserID"] = 10000;
            BandGV();
            
            
           
        }
    }
    protected override void InitPage()
    {
        gv_celue.Columns[0].HeaderText = GetLanageValueByKey("msg_CelueSelectQuest_xuanze", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[2].HeaderText = GetLanageValueByKey("msg_CelueSelectQuest_questtypez1", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[4].HeaderText = GetLanageValueByKey("msg_CelueSelectQuest_questcountz1", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[6].HeaderText = GetLanageValueByKey("msg_CelueSelectQuest_Coursetypez1", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[3].HeaderText = GetLanageValueByKey("msg_CelueSelectQuest_nanduzhiz1", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[7].HeaderText = GetLanageValueByKey("lab_AddQuestionInfo_danx_Ken", PowerCreator.LMS.Language.LanagetItemType.Page);
        gv_celue.Columns[5].HeaderText = GetLanageValueByKey("Msg_ManagerQuestion_QuestionScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        


        lab_TitleInfo.Text = GetLanageValueByKey("lab_BuilderPaper_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_add.Text = GetLanageValueByKey("btn_CelueSelectQuestion_SaveCelue", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_Msg.Text = GetLanageValueByKey("lab_CelueSelectQuestion_MsgText", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BandGV()
    {
        DataTable dt_gv = new DataTable();
        dt_gv.Columns.Add("ID");

        DataTable dt_Course = new DataTable();
        dt_Course = objCourse.Query("");

        #region  加入基础数据
        DataTable dt_QuestionType = new DataTable();
        dt_QuestionType.Columns.Add("TypeID");
        dt_QuestionType.Columns.Add("TypeName");

        DataRow dr11 = dt_QuestionType.NewRow();
        dr11["TypeID"] = "1";
        dr11["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
        dt_QuestionType.Rows.Add(dr11);
        DataRow dr2 = dt_QuestionType.NewRow();
        dr2["TypeID"] = "2";
        dr2["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        dt_QuestionType.Rows.Add(dr2);
        DataRow dr3 = dt_QuestionType.NewRow();
        dr3["TypeID"] = "3";
        dr3["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        dt_QuestionType.Rows.Add(dr3);
        DataRow dr4 = dt_QuestionType.NewRow();
        dr4["TypeID"] = "4";
        dr4["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
        dt_QuestionType.Rows.Add(dr4);
        DataRow dr5 = dt_QuestionType.NewRow();
        dr5["TypeID"] = "5";
        dr5["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_mingchiti", PowerCreator.LMS.Language.LanagetItemType.Page);
        //dt_QuestionType.Rows.Add(dr4);
        dt_QuestionType.Rows.Add(dr5);
        DataRow dr6 = dt_QuestionType.NewRow();
        dr6["TypeID"] = "6";
        dr6["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
        dt_QuestionType.Rows.Add(dr6);

        DataRow dr7 = dt_QuestionType.NewRow();
        dr7["TypeID"] = "7";
        dr7["TypeName"] = GetLanageValueByKey("msg_CelueSelectQuest_zuheti", PowerCreator.LMS.Language.LanagetItemType.Page);
        dt_QuestionType.Rows.Add(dr7);
        #endregion


        for (int i = 0; i <13; i++)
        {
            DataRow dr1 = dt_gv.NewRow();
            dr1["ID"] = Guid.NewGuid();

            dt_gv.Rows.Add(dr1);
        }
        gv_celue.DataSource = dt_gv;
        gv_celue.DataBind();
        for (int i = 1; i < 14; i++)
        {
            Label lab_QuestionType = (Label)gv_celue.Rows[i - 1].Cells[2].FindControl("lab_QuestionType");
            Label lab_QuestionTypeID = (Label)gv_celue.Rows[i - 1].Cells[2].FindControl("lab_QuestionTypeID");
            lab_QuestionTypeID.Text = i.ToString();
            //switch (i)
            //{
            //    case 1: lab_QuestionType.Text = "判断题";

            //        break;
            //    case 2: lab_QuestionType.Text = "单选题";
            //        break;
            //    case 3: lab_QuestionType.Text = "多选题";
            //        break;
            //    case 4: lab_QuestionType.Text = "填空题";
            //        break;
            //    case 5: lab_QuestionType.Text = "名词解释";
            //        break;
            //    default:
            //        lab_QuestionType.Text = "简答题";
            //        break;
            //}

            //ddlQuestionType
            DropDownList ddlQuestionType = (DropDownList)gv_celue.Rows[i - 1].Cells[6].FindControl("ddlQuestionType");
            ddlQuestionType.DataSource = dt_QuestionType;
            ddlQuestionType.DataTextField = "TypeName";
            ddlQuestionType.DataValueField = "TypeID";
            ddlQuestionType.DataBind();

            DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i - 1].Cells[6].FindControl("dpdlst_classtype");
            dpdlst_classtype.DataSource = dt_Course;
            dpdlst_classtype.DataValueField = "Course_Id";
            dpdlst_classtype.DataTextField = "CourseName";
            dpdlst_classtype.DataBind();
        }
    }
    private bool CheckDate()
    {
        Hashtable ht = new Hashtable();
        Hashtable ht1 = new Hashtable();

        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();

        ArrayList list3 = new ArrayList();
        Hashtable ht3 = new Hashtable();


        for (int i = 0; i < gv_celue.Rows.Count; i++)
        {
            CheckBox chbox_check = (CheckBox)gv_celue.Rows[i].Cells[0].FindControl("chbox_check");
            if (chbox_check.Checked)
            {
                Label lab_QuestionTypeID = (Label)gv_celue.Rows[i].Cells[2].FindControl("lab_QuestionTypeID");
                Label lab_QuestionType = (Label)gv_celue.Rows[i].Cells[2].FindControl("lab_QuestionType");

                DropDownList ddlQuestionType = (DropDownList)gv_celue.Rows[i].Cells[2].FindControl("ddlQuestionType");
                lab_QuestionTypeID.Text = ddlQuestionType.SelectedValue;
                lab_QuestionType.Text = ddlQuestionType.SelectedItem.Text;

                DropDownList dpdlist_nanduzhi = (DropDownList)gv_celue.Rows[i].Cells[3].FindControl("dpdlist_nanduzhi");

                DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i].Cells[6].FindControl("dpdlst_classtype");

                TextBox txt_Ken = (TextBox)gv_celue.Rows[i].Cells[7].FindControl("txt_Ken");

                TextBox txt_QuestionCount = (TextBox)gv_celue.Rows[i].Cells[4].FindControl("txt_QuestionCount");
                TextBox txt_ScoreCount = (TextBox)gv_celue.Rows[i].Cells[5].FindControl("txt_ScoreCount");

                if (string.IsNullOrEmpty(txt_QuestionCount.Text))
                { 
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti1", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
                if (string.IsNullOrEmpty(txt_ScoreCount.Text))
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti12", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }

                if (!PageValidate.IsNumber(txt_QuestionCount.Text))
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti13", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }

                if (!PageValidate.IsNumber(txt_ScoreCount.Text))
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti14", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }

                if (txt_QuestionCount.Text == "0" | txt_ScoreCount.Text == "0")
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti15", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
                int QuestionCount = int.Parse(txt_QuestionCount.Text);
                int Diffculty = int.Parse(dpdlist_nanduzhi.SelectedValue);
                int QuestionTypeID = int.Parse(lab_QuestionTypeID.Text);
                int ScoreCount = int.Parse(txt_ScoreCount.Text);
                int CourseID = int.Parse(dpdlst_classtype.SelectedValue);
                string Ken = txt_Ken.Text;
                int questionCount = objManagerPaper.GetAoutSelectQuestion(QuestionCount, Diffculty, QuestionTypeID, ScoreCount, CourseID, Ken).Rows.Count;
                if (questionCount < QuestionCount)
                {
                    Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti16", PowerCreator.LMS.Language.LanagetItemType.Page) + lab_QuestionType.Text + GetLanageValueByKey("msg_CelueSelectQuest_shuruti17", PowerCreator.LMS.Language.LanagetItemType.Page)+ GetLanageValueByKey("msg_CelueSelectQuest_shuruti18", PowerCreator.LMS.Language.LanagetItemType.Page) + questionCount.ToString() + GetLanageValueByKey("msg_CelueSelectQuest_shuruti19", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
                string key = QuestionTypeID.ToString() + "," + CourseID.ToString();
                string keyv = Diffculty.ToString();

                try
                {
                    ht3.Add(key + "," + keyv, keyv);
                }
                catch (Exception )
                {
                    Script.Alert(Page, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti10", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }

                if (keyv == "-1")
                {
                    ht1.Add(key, keyv);
                    list1.Add(key);
                }
                else
                {
                    // ht.Add(key, keyv);
                    list2.Add(key);
                }
            }
        }

        for (int i = 0; i < list1.Count; i++)
        {
            list1[i].ToString();
            for (int j = 0; j < list2.Count; j++)
            {
                if (list2[j].ToString() == list1[i].ToString())
                {
                    Script.Alert(Page, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti21", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return false;
                }
            }

        }
        return true;
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        int sumPaperScore = 0;
        if (!CheckDate())
        {
            return;
        }
        VOPloy objPloyInfo = new VOPloy();
        
        
        //objPloyInfo.CelueName = txt_PloyName.Text;
        objPloyInfo.PloyCreateID = int.Parse(ViewState["UserID"].ToString());
        objPloyInfo.PloyCreateTime = DateTime.Now;
        int PloyID = 0;
        objManagerPaper.CreatePloy(objPloyInfo, ref PloyID);



        VOPloyOption objPloyOptionInfo = new VOPloyOption();
        objPloyOptionInfo.FKPloyOptionPloyID = PloyID;
        int checkCount = 0;
        for (int i = 0; i < gv_celue.Rows.Count; i++)
        {
            CheckBox chbox_check = (CheckBox)gv_celue.Rows[i].Cells[0].FindControl("chbox_check");
            if (chbox_check.Checked)
            {

                Label lab_QuestionTypeID = (Label)gv_celue.Rows[i].Cells[2].FindControl("lab_QuestionTypeID");
                
                DropDownList ddlQuestionType = (DropDownList)gv_celue.Rows[i].Cells[2].FindControl("ddlQuestionType");
                lab_QuestionTypeID.Text = ddlQuestionType.SelectedValue;

                objPloyOptionInfo.PloyOptionQuestionTypeID = int.Parse(lab_QuestionTypeID.Text);
                DropDownList dpdlist_nanduzhi = (DropDownList)gv_celue.Rows[i].Cells[3].FindControl("dpdlist_nanduzhi");
                objPloyOptionInfo.PloyOptionDiffculty = int.Parse(dpdlist_nanduzhi.SelectedValue);
                TextBox txt_QuestionCount = (TextBox)gv_celue.Rows[i].Cells[4].FindControl("txt_QuestionCount");
                objPloyOptionInfo.PloyOptionQuestionCount = int.Parse(txt_QuestionCount.Text.Trim());
             
                TextBox txt_ScoreCount = (TextBox)gv_celue.Rows[i].Cells[5].FindControl("txt_ScoreCount");

                objPloyOptionInfo.PloyOptionQuestionScore = int.Parse(txt_ScoreCount.Text.Trim());


                DropDownList dpdlst_classtype = (DropDownList)gv_celue.Rows[i].Cells[6].FindControl("dpdlst_classtype");
                objPloyOptionInfo.FKPloyOptionCourseID = int.Parse(dpdlst_classtype.SelectedValue);
                TextBox txt_Ken = (TextBox)gv_celue.Rows[i].Cells[7].FindControl("txt_Ken");
                objPloyOptionInfo.PloyOptionKen = txt_Ken.Text;
                objPloyOptionInfo.PloyOptionCreateID = int.Parse(ViewState["UserID"].ToString());
                objPloyOptionInfo.PloyOptionCreateTime = DateTime.Now;
                sumPaperScore += int.Parse(txt_QuestionCount.Text) * int.Parse(txt_ScoreCount.Text);
                objManagerPaper.CreatePloyOption(objPloyOptionInfo);
                checkCount += 1;
            }
        }
        if (checkCount == 0)
        {
            Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti22", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        VOPaper objPaperInfo = new VOPaper();
        objPaperInfo = objManagerPaper.LoadPaper(int.Parse(ViewState["PaperID"].ToString()));
        objPaperInfo.PaperCelueID = PloyID;
        objPaperInfo.PaperScore = sumPaperScore;
        objPaperInfo.PaperBuildState = 1;

        if (objManagerPaper.UpdatePaper(objPaperInfo))
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerPaperInfo.aspx");
        }
        else
        {
            Script.ShowAndRedirect(this, GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page), "ManagerPaperInfo.aspx");
        }

        
    }
}
